import apis from "../services/addPlanApi";

export default {
  namespace: "addPlan",
  state: {
    addErrCode: "-1",
    modErrCode: "-1",
    detailPlan: {},
    channelList: [],
    loading: false,
    errMsg: "",
    planAudit: {},
    releaseErr: -1,
    auditPlanErr: -1
  },
  subscriptions: {
    // 监听地址工作台
    setup({ dispatch, history }) {
      history.listen(location => {
        if (
          location.pathname === "/intellPush/recommendCenter/plan/addPlan" ||
          location.pathname === "/intellPush/recommendCenter/plan/editPlan"
        ) {
          if (location.search.indexOf("key") >= 0) {
            const key = JSON.parse(
              window.decodeURIComponent(location.search).substring(5)
            );
            dispatch({ type: "put/putkey", payload: key });
            // if (key.operate === "mod") {
            //   dispatch({
            //     type: "detailPlan",
            //     payload: { planId: key.planId }
            //   });
            // }
          }
        }
        if (location.pathname.includes("/planDetail")) {
          if (location.search.indexOf("key") >= 0) {
            const key = JSON.parse(
              window.decodeURIComponent(location.search).substring(5)
            );
            dispatch({ type: "put/putplanAudit", payload: key });
            // if (key.operate === "mod") {
            //   dispatch({
            //     type: "detailPlan",
            //     payload: { planId: key.planId }
            //   });
            // }
          }
        }
      });
    }
  },
  effects: {
    // 推送数量趋势图
    *addplanList({ payload }, { call, put }) {
      const res = yield call(apis.addplanList, payload);
      const { errCode = "-1", errMsg } = res;
      yield put({
        type: "put/addError",
        payload: errCode
      });
      yield put({
        type: "put/errMsg",
        payload: errMsg
      });
    },
    *modplanList({ payload }, { call, put }) {
      const res = yield call(apis.modplanList, payload);
      const { errCode = "-1", errMsg } = res;
      yield put({
        type: "put/modError",
        payload: errCode
      });
      yield put({
        type: "put/errMsg",
        payload: errMsg
      });
    },
    *detailPlan({ payload }, { call, put }) {
      yield put({
        type: "put/loading",
        payload: true
      });
      const res = yield call(apis.detailPlan, payload);
      const { datas = {} } = res;
      yield put({
        type: "put/detail",
        payload: datas
      });
      yield put({
        type: "put/loading",
        payload: false
      });
    },
    *getPushChannel({ payload }, { call, put }) {
      const res = yield call(apis.pushChannel, payload);
      const { datas = [] } = res;
      yield put({
        type: "put/pushChannel",
        payload: datas
      });
    },
    *releasePlan({ payload }, { call, put }) {
      const res = yield call(apis.releasePlan, payload);
      const { errCode, errMsg } = res;
      yield put({
        type: "put/releasePlan",
        payload: errCode
      });
      yield put({
        type: "put/errMsg",
        payload: errMsg
      });
    },
    *auditPlan({ payload }, { call, put }) {
      const res = yield call(apis.auditPlan, payload);
      const { errCode } = res;
      yield put({
        type: "put/auditPlan",
        payload: errCode
      });
    }
  },
  reducers: {
    "put/addError": (state, { payload }) => ({
      ...state,
      addErrCode: payload
    }),
    "put/modError": (state, { payload }) => ({
      ...state,
      modErrCode: payload
    }),
    "put/detail": (state, { payload }) => ({
      ...state,
      detailPlan: payload
    }),
    "put/putkey": (state, { payload }) => ({
      ...state,
      key: payload
    }),
    "put/pushChannel": (state, { payload }) => ({
      ...state,
      channelList: payload
    }),
    "put/loading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/errMsg": (state, { payload }) => ({
      ...state,
      errMsg: payload
    }),
    "put/putplanAudit": (state, { payload }) => ({
      ...state,
      planAudit: payload
    }),
    "put/releasePlan": (state, { payload }) => ({
      ...state,
      releaseErr: payload
    }),
    "put/auditPlan": (state, { payload }) => ({
      ...state,
      auditPlanErr: payload
    })
  }
};
